package com.example.a04;

import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.context.support.GenericApplicationContext;

/*
    bean后处理器作用
 */
public class Application04 {
    public static void main(String[] args) {
        // GenericApplicationContext：一个干净的容器
        GenericApplicationContext context = new GenericApplicationContext();

        // 用原始方法注册三个bean
        context.registerBean(Bean1.class);
        context.registerBean(Bean2.class);
        context.registerBean(Bean3.class);
        context.registerBean(Bean4.class);

        context.getDefaultListableBeanFactory().setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
        // 解析@Autowire、@Value注解
        context.registerBean(AutowiredAnnotationBeanPostProcessor.class);
        // 解析@Resource、@PostConstruct、@PreDestroy注解
        context.registerBean(CommonAnnotationBeanPostProcessor.class);
        // 解析@ConfigurationPorperties注解
        ConfigurationPropertiesBindingPostProcessor.register(context.getDefaultListableBeanFactory());

        // 初始化容器
        context.refresh();

        System.out.println(context.getBean(Bean4.class));
        // 销毁容器
        context.close();
    }
}
